﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace foldshot.View
{
    public partial class FoldTreePanel : UserControl
    {
        private FileInfo File1;

        private bool isBinded = false;

        public FoldTreePanel()
        {
            InitializeComponent();
        }

        public event Action OnFileOppened;

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File1 = new FileInfo(this.openFileDialog.FileName);
                this.fileNameLabel.Text = File1.Name;
                this.toolTip.SetToolTip(this.fileNameLabel, File1.FullName + "\nlastmodified: "
                    + File1.LastWriteTime.ToShortDateString()
                    + " "
                    + File1.LastWriteTime.ToShortTimeString());

                OnFileOppened();
            }
        }

        public void TooltipNode(DiffTreeNode node)
        {
            this.statusLabel.Text = node.genShortDescr();
            this.toolTip.SetToolTip(this.statusLabel, node.genShortDescr());
        }

        public void UnooltipNode()
        {
            this.statusLabel.Text = "";
            this.toolTip.SetToolTip(this.statusLabel, null);
        }


        public FileInfo File
        {
            get { return File1; }
        }

        public FoldTreeView FoldTreeView
        {
            get { return foldTreeView; }
        }


        public void BindTo(FoldTreePanel another)
        {

            if (!isBinded)
            {

                this.foldTreeView.BindTo(another.FoldTreeView);


                foldTreeView.AfterSelect += (sender, e) =>
                                                {
                                                    var node = e.Node as DiffTreeNode;
                                                    if (node != null)
                                                    {
                                                        this.TooltipNode(node);
                                                        another.TooltipNode(node.BrotherNode);
                                                    }
                                                };

                foldTreeView.Leave += (sender, e) =>
                                          {
                                              this.UnooltipNode();
                                              another.UnooltipNode();
                                          };

                isBinded = true;
            }



        }

        private void foldTreeView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
           var node = foldTreeView.GetNodeAt(e.Location) as DiffTreeNode;
           if (node != null)
           {
               var dirnode = new DirectoryInfo(node.File.absolutePath);
               if (dirnode.Exists)
               {
                   Console.Out.WriteLine("Opening: " + dirnode.FullName);
                   Process.Start(dirnode.FullName);
               }
               else
               {
                   Console.Out.WriteLine("folder " + node.File.absolutePath + " does not exist");

                   var fn = new FileInfo(node.File.absolutePath);
                   if (fn.Exists)
                   {
                       var dir = fn.Directory;

                       if (dir != null)
                       {
                           if (dir.Exists)
                           {
                               Console.Out.WriteLine("Opening: " + dir.FullName);
                               Process.Start(dir.FullName);
                           }
                           else
                           {
                               Console.Out.WriteLine("folder " + dir.FullName + " does not exist");
                           }
                       }
                       else
                       {
                           Console.Out.WriteLine("folder for file " + node.File.absolutePath + " does not exist");
                       }
                   }
               }
           }

        }

    }
}
